package com.windchime.blog.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.windchime.blog.entity.Admin;
import com.windchime.blog.mapper.AdminMapper;
import com.windchime.blog.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author WindChime
 * @since 2021-07-31
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    /**
     * 根据用户名密码查询管理员
     * @param username 用户名
     * @param password 密码
     * @return 管理员
     */
    @Override
    public Admin getAdminByUsernameAndPassword(String username, String password) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        wrapper.eq("password", SecureUtil.md5(password));
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 根据ID旧密码查询管理员
     * @param id           ID
     * @param old_password 旧密码
     * @return 管理员
     */
    @Override
    public Admin getAdminByIdAndPassword(Integer id, String old_password) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("password", SecureUtil.md5(old_password));
        return baseMapper.selectOne(wrapper);
    }
}
